/**
 * 
 */
package com.qjs.smterp.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.model.LazyDataModel;

import com.qjs.smterp.web.beans.MbAuthentication;
import com.qjs.smterp.web.utils.FacesContextUtils;
import com.qjs.smterp.dto.GoodsTmp;
import com.qjs.smterp.model.Employee;
import com.qjs.smterp.model.Goods;
import com.qjs.smterp.model.GoodsInstock;
import com.qjs.smterp.model.Instock;
import com.qjs.smterp.model.Stock;
import com.qjs.smterp.model.Unit;
import com.qjs.smterp.model.Users;
import com.qjs.smterp.services.IEMService;
import com.qjs.smterp.utils.NumberUtils;

/**
 * @author Vostro 3400
 * 
 */
@ManagedBean(name = "mbInStock")
@ViewScoped
public class MbInStock implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@ManagedProperty("#{iemService}")
	private IEMService iemService;

	private List<Instock> instocks;
	private Instock[] selectedInStocks;
	private Instock selectedInstock;
	private List<Unit> units;
	private Goods goods;
	private Unit unit;
	private Stock stock;
	private Employee employee;
	private Instock instock;
	private GoodsTmp goodsTmp;
	private GoodsTmp[] goodsTmpsArray;
	private GoodsInstock goodsInstock;
	private List<GoodsTmp> goodsTmps;
	private Users userLogin;

	private List<Goods> listGoods;
	private List<Stock> listStocks;
	private List<Employee> listEmployees;
	private Map<String, Integer> mapGoods;
	private Map<String, Integer> mapUnits;
	private Map<String, Integer> mapStocks;

	private Map<String, Integer> mapEmployees;

	private LazyDataModel<Instock> lazyInstockModel;

	@PostConstruct
	public void init() {
		instocks = getAllInStock();
		mapGoods = getMapAllGoods();
		goods = new Goods();
		mapUnits = getMapAllUnits();
		unit = new Unit();
		mapStocks = getMapAllStocks();
		stock = new Stock();
		employee = new Employee();
		mapEmployees = getMapAllEmployees();
		listGoods = new ArrayList<Goods>();
		goodsTmp = new GoodsTmp();
		goodsTmps = new ArrayList<GoodsTmp>();
		goodsInstock = new GoodsInstock();
		instock = new Instock();
		userLogin = (Users) FacesContextUtils
				.getSessionObject(MbAuthentication.AUTH_KEY);
	}

	public MbInStock() {
		System.out.println("<---------------------------------->");
	}

	public List<Instock> getAllInStock() {
		return iemService.findALlInstock();
	}

	public List<Stock> getAllStocks() {
		return iemService.findALlStock();
	}

	public List<Employee> getAllEmployees() {
		return iemService.findALlEmployees();
	}

	public Map<String, Integer> getMapAllGoods() {
		Map<String, Integer> mgoods = new HashMap<String, Integer>();
		List<Goods> temps = getAllGoods();
		for (Goods good : temps) {
			mgoods.put(good.getGoodsCode() + ":" + good.getGoodsName(),
					good.getGoodsId());
		}
		return mgoods;
	}

	public Map<String, Integer> getMapAllUnits() {
		Map<String, Integer> unitMap = new HashMap<String, Integer>();
		List<Unit> unittmp = getAllUnits();
		for (Unit unit : unittmp) {
			unitMap.put(unit.getUnitCode() + ":" + unit.getUnitName(),
					unit.getUnitId());
		}
		return unitMap;
	}

	public Map<String, Integer> getMapAllEmployees() {
		Map<String, Integer> eMap = new HashMap<String, Integer>();
		List<Employee> etmp = getAllEmployees();
		for (Employee employee : etmp) {
			eMap.put(employee.getSalutation() + "." + employee.getFirstname()
					+ " " + employee.getLastname(), employee.getEmployeeId());
		}
		return eMap;
	}

	public Map<String, Integer> getMapAllStocks() {
		Map<String, Integer> stockMap = new HashMap<String, Integer>();
		List<Stock> stocktmp = getAllStocks();
		for (Stock stock : stocktmp) {
			stockMap.put(stock.getStockCode() + ":" + stock.getStockName(),
					stock.getStockId());
		}
		return stockMap;
	}

	public List<Goods> getAllGoods() {
		return iemService.findALlGoods();
	}

	public List<Unit> getAllUnits() {
		return iemService.findALlUnits();
	}

	public void addGoodsToInstock(ActionEvent event) {
		GoodsTmp tmp = new GoodsTmp();
		if (goodsTmp.getGoodsId().intValue() != 0) {
			Goods goods = iemService.findByGoodsId(goodsTmp.getGoodsId());
			Unit unit = iemService.findByUnitId(goodsTmp.getUnitId());
			float total = 0;

			tmp.setGoodsId(goods.getGoodsId());
			tmp.setGoodsCode(goods.getGoodsCode());
			tmp.setGoodsName(goods.getGoodsName());
			tmp.setUnitCode(unit.getUnitCode());
			tmp.setUnitId(unit.getUnitId());
			tmp.setQuantity(goodsTmp.getQuantity());
			tmp.setUnitPrice(goodsTmp.getUnitPrice());
			tmp.setTotal(goodsTmp.getQuantity() * goodsTmp.getUnitPrice());

			boolean goodsAvailable = false;
			if (goodsTmps.size() > 0) {
				for (int i = 0; i < goodsTmps.size(); i++) {
					GoodsTmp test = goodsTmps.get(i);
					if (test.getGoodsId().intValue() == tmp.getGoodsId()
							.intValue()) {
						goodsAvailable = true;
						float ftest = tmp.getQuantity() + test.getQuantity();
						tmp.setQuantity(ftest);
						tmp.setTotal(ftest * test.getUnitPrice());
						goodsTmps.set(i, tmp);
					}
				}
				if (goodsAvailable == false) {
					goodsTmps.add(tmp);
				}
			} else {
				goodsTmps.add(tmp);
			}

			for (GoodsTmp tmp2 : goodsTmps) {
				total += tmp2.getTotal();
			}
			instock.setGrandTotal(total);
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
							"Vui lòng chọn hàng hóa cho phiếu nhập"));
		}
	}

	public void removeGoodsFromInstock(ActionEvent event){
		for (GoodsTmp gTmp : goodsTmpsArray) {
			goodsTmps.remove(gTmp);
		}
	}
	public void loadInfoToAdd(ActionEvent event) {
		instock.setInstockCode(NumberUtils.generateInStockCode("INS"));
	}

	public void valueChanged() {
		Goods goods = iemService.findByGoodsId(goodsTmp.getGoodsId());
		goodsTmp.setGoodsCode(goods.getGoodsCode());

	}

	public void testGoodsInstock(ActionEvent event) {

		if (instock.getDocCode().equals("")
				|| instock.getInstockDate().equals("")) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
							"Không thể lưu. Vui lòng nhập đầy đủ thông tin phiếu nhập kho."));

		} else {
			Instock instocktmp = instock;
			stock = iemService.findByStockId(stock.getStockId());
			instocktmp.setChiefAcc(stock.getStockeeperId());
			instocktmp.setStorekeeperName(stock.getStockeeperName());
			instocktmp.setCreatedBy(userLogin.getLogin());
			instocktmp.setDateCreated(new Date());

			iemService.createInstock(instocktmp);

			for (GoodsTmp goods : goodsTmps) {
				GoodsInstock goodsInstock = new GoodsInstock();
				goodsInstock.setGoodsid(goods.getGoodsId());
				goodsInstock.setGoodsCode(goods.getGoodsCode());
				goodsInstock.setGoodsName(goods.getGoodsName());
				goodsInstock.setInstockid(instocktmp.getInstockId());
				goodsInstock.setNumbersCurrent(goods.getQuantity());
				goodsInstock.setTotalPrice(goods.getTotal());
				goodsInstock.setUnitPrice(goods.getUnitPrice());
				goodsInstock.setUnitid(goods.getUnitId());
				goodsInstock.setUnitCode(goods.getUnitCode());
				goodsInstock.setStockid(stock.getStockId());
				goodsInstock.setStockName(stock.getStockName());

				goodsInstock.setCreatedBy(userLogin.getLogin());
				goodsInstock.setDateCreated(new Date());
				iemService.createGoodsInstock(goodsInstock);
			}
			instocks = getAllInStock();
			instock = new Instock();
			goodsTmp = new GoodsTmp();
			goodsTmps = new ArrayList<GoodsTmp>();
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "",
							"Thêm phiếu nhập kho thành công."));
		}

	}

	/**
	 * @param iemService
	 *            the iemService to set
	 */
	public void setIemService(IEMService iemService) {
		this.iemService = iemService;
	}

	/**
	 * @return the instocks
	 */
	public List<Instock> getInstocks() {
		return instocks;
	}

	/**
	 * @param instocks
	 *            the instocks to set
	 */
	public void setInstocks(List<Instock> instocks) {
		this.instocks = instocks;
	}

	/**
	 * @return the selectedInStocks
	 */
	public Instock[] getSelectedInStocks() {
		return selectedInStocks;
	}

	/**
	 * @param selectedInStocks
	 *            the selectedInStocks to set
	 */
	public void setSelectedInStocks(Instock[] selectedInStocks) {
		this.selectedInStocks = selectedInStocks;
	}

	/**
	 * @return the selectedInstock
	 */
	public Instock getSelectedInstock() {
		return selectedInstock;
	}

	/**
	 * @param selectedInstock
	 *            the selectedInstock to set
	 */
	public void setSelectedInstock(Instock selectedInstock) {
		this.selectedInstock = selectedInstock;
	}

	/**
	 * @return the units
	 */
	public List<Unit> getUnits() {
		return units;
	}

	/**
	 * @param units
	 *            the units to set
	 */
	public void setUnits(List<Unit> units) {
		this.units = units;
	}

	/**
	 * @return the listGoods
	 */
	public List<Goods> getListGoods() {
		return listGoods;
	}

	/**
	 * @param listGoods
	 *            the listGoods to set
	 */
	public void setListGoods(List<Goods> listGoods) {
		this.listGoods = listGoods;
	}

	/**
	 * @return the mapGoods
	 */
	public Map<String, Integer> getMapGoods() {
		return mapGoods;
	}

	/**
	 * @param mapGoods
	 *            the mapGoods to set
	 */
	public void setMapGoods(Map<String, Integer> mapGoods) {
		this.mapGoods = mapGoods;
	}

	/**
	 * @return the goods
	 */
	public Goods getGoods() {
		return goods;
	}

	/**
	 * @param goods
	 *            the goods to set
	 */
	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	/**
	 * @return the mapUnits
	 */
	public Map<String, Integer> getMapUnits() {
		return mapUnits;
	}

	/**
	 * @param mapUnits
	 *            the mapUnits to set
	 */
	public void setMapUnits(Map<String, Integer> mapUnits) {
		this.mapUnits = mapUnits;
	}

	/**
	 * @return the unit
	 */
	public Unit getUnit() {
		return unit;
	}

	/**
	 * @param unit
	 *            the unit to set
	 */
	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	/**
	 * @return the stock
	 */
	public Stock getStock() {
		return stock;
	}

	/**
	 * @param stock
	 *            the stock to set
	 */
	public void setStock(Stock stock) {
		this.stock = stock;
	}

	/**
	 * @return the listStocks
	 */
	public List<Stock> getListStocks() {
		return listStocks;
	}

	/**
	 * @param listStocks
	 *            the listStocks to set
	 */
	public void setListStocks(List<Stock> listStocks) {
		this.listStocks = listStocks;
	}

	/**
	 * @return the mapStocks
	 */
	public Map<String, Integer> getMapStocks() {
		return mapStocks;
	}

	/**
	 * @param mapStocks
	 *            the mapStocks to set
	 */
	public void setMapStocks(Map<String, Integer> mapStocks) {
		this.mapStocks = mapStocks;
	}

	/**
	 * @return the employee
	 */
	public Employee getEmployee() {
		return employee;
	}

	/**
	 * @param employee
	 *            the employee to set
	 */
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	/**
	 * @return the listEmployees
	 */
	public List<Employee> getListEmployees() {
		return listEmployees;
	}

	/**
	 * @param listEmployees
	 *            the listEmployees to set
	 */
	public void setListEmployees(List<Employee> listEmployees) {
		this.listEmployees = listEmployees;
	}

	/**
	 * @return the mapEmployees
	 */
	public Map<String, Integer> getMapEmployees() {
		return mapEmployees;
	}

	/**
	 * @param mapEmployees
	 *            the mapEmployees to set
	 */
	public void setMapEmployees(Map<String, Integer> mapEmployees) {
		this.mapEmployees = mapEmployees;
	}

	/**
	 * @return the goodsTmp
	 */
	public GoodsTmp getGoodsTmp() {
		return goodsTmp;
	}

	/**
	 * @param goodsTmp
	 *            the goodsTmp to set
	 */
	public void setGoodsTmp(GoodsTmp goodsTmp) {
		this.goodsTmp = goodsTmp;
	}

	/**
	 * @return the goodsTmps
	 */
	public List<GoodsTmp> getGoodsTmps() {
		return goodsTmps;
	}

	/**
	 * @param goodsTmps
	 *            the goodsTmps to set
	 */
	public void setGoodsTmps(List<GoodsTmp> goodsTmps) {
		this.goodsTmps = goodsTmps;
	}

	public GoodsInstock getGoodsInstock() {
		return goodsInstock;
	}

	public void setGoodsInstock(GoodsInstock goodsInstock) {
		this.goodsInstock = goodsInstock;
	}

	public Instock getInstock() {
		return instock;
	}

	public void setInstock(Instock instock) {
		this.instock = instock;
	}

	public LazyDataModel<Instock> getLazyInstockModel() {
		return lazyInstockModel;
	}

	public GoodsTmp[] getGoodsTmpsArray() {
		return goodsTmpsArray;
	}

	public void setGoodsTmpsArray(GoodsTmp[] goodsTmpsArray) {
		this.goodsTmpsArray = goodsTmpsArray;
	}

}
